
function TreeViewRender(tree,displayDegree,showChapterNo){
	this.tree = tree ;
	this.id = TreeViewRender.defaultInstanceName ;
	this.displayDegree = displayDegree==null?1:displayDegree ;
	this.showChapterNo = showChapterNo == null ? true : showChapterNo;
	this.currentFocusNodeId = null ;
	this.fakeAAZoneId = "";

	for(var i=0;i<this.tree.nodes.size();i++){
		var node = this.tree.nodes.get(i) ;
		node.opened = node.rank < this.displayDegree ;
	}
	for(var i=0;i<this.tree.nodes.size();i++){
		var node = this.tree.nodes.get(i) ;
		node.refreshDisplay() ;
	}
	return this ;
}

TreeViewRender.defaultInstanceName = "TVR";

TreeViewRender.prototype.bindById = function () {
    var key = "TreeViewRender." + this.id;
    window[key] = this;
}

function findTreeViewRenderInstance() {
    var key = "TreeViewRender." + TreeViewRender.defaultInstanceName;
    return window[key];
}

TreeViewRender.prototype.focusNode = function(id){
	if(this.currentFocusNodeId != null){
		this.tree.findNodeById(this.currentFocusNodeId).focused = false;
	}
	this.currentFocusNodeId = id ;
	this.tree.findNodeById(this.currentFocusNodeId).focused = true;
}

TreeViewRender.prototype.refresh = function(){
//	this.tree.init();
	var html = this.getTreeHTML() ;
//	document.write(html);
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeViewRender.prototype.render = function(){
	if(!this.tree.neatened){
		this.tree.init();
	}
	var html = this.getTreeHTML() ;
	if(this.fakeAAZoneId != null && this.fakeAAZoneId != ""){
		try{
			var node = parent.document.getElementById(this.fakeAAZoneId);
			if(node != null){
				node.innerHTML = html ;
				// alert("refresh parent tree .");
				return ;
			}
		}catch(e){
		}
	}
	document.write("<div id='DivTree' style='width:100%;height:100%;text-align:left;vertical-align:top;'></div>");
	var div = document.getElementById("DivTree");
	div.innerHTML = html ;
}

TreeViewRender.prototype.getTreeHTML = function(){
	var sb = new Stringsb();
	sb.append("<table cellspacing=0 cellpadding=0 style='border:0px;'>");
	for(var i=0;i<this.tree.nodes.size();i++){
		sb.append(this.getTreeNodeHTML(this.tree.nodes.get(i)));
	}
	sb.append("</table>");
	return sb.toString();
}

TreeViewRender.prototype.getTreeNodeHTML = function(node){
	if(!node.display){
		return "";
	}
	var sb = new Stringsb();

	sb.append("<tr id='").append(this.id).append("_ROW_").append(node.id).append("'>");
	sb.append("<td style='height:18px;padding:0px;'>");
	
	sb.append("<table cellspacing=0 cellpadding=0");
	if(node.method != null && node.method != ""){
		sb.append(" onclick=\"").append(node.method).append("\"");
	}
	sb.append(" style=''>");
	sb.append("<tr>");
	var lines = node.lines ;
	for(var i=0;i<lines.length;i++){
		if(lines[i] == "1"){
			sb.append("<td><div class='YHMenu_nodeLine'></div></td>");
		}else{
			sb.append("<td><div class='YHMenu_nodeEmpty'></div></td>");
		}
	}
	sb.append("<td class='").append(this.getNodeCss(node)).append("' onclick=\"changeTreeView('").append(node.id).append("')\"><div style='width:18px;'></div></td>");
	sb.append("<td><div class='YHMenu_nodeIcon' style='background-image:url(").append(node.icon).append(");background-repeat:no-repeat;background-position:0% 50%;' class='menuIcon'></div></td>");
	sb.append("<td id='_TEXT_NODE_").append(node.id).append("' nodeId='").append(node.id).append("'");
	sb.append(" class='").append(node.getTextNodeCss()).append("' onmouseover='overTextNode(this)' onmouseout='outTextNode(this)' onclick='clickTextNode(this)'>");
	sb.append("<div class='divTextNode'>");
	if(this.showChapterNo){
		sb.append(node.chapterNo).append(" ");
	}
	sb.append(node.name);
	sb.append("</div>");
	sb.append("</td>");


	sb.append("</tr>");
	sb.append("</table>");

	sb.append("</td>");
	sb.append("</tr>");
	
	return sb.toString();
}

TreeViewRender.prototype.getNodeCss = function(node){
	var open = node.opened ? '1':'0';
	var str = "YHMenu_node" + open + (node.hasYoungerBrother()?"1":"0") + (node.hasChild()?"1":"0");
	return str ;
}

TreeViewRender.prototype.clickNode = function(nodeId){
	alert("please override me: "+nodeId);
}

function changeTreeView(id){
	var viewRender = findTreeViewRenderInstance();
	var node = viewRender.tree.findNodeById(id);
	if(node.sequence >= node.shadowIndex){
		return ;
	}
	node.opened = !node.opened ;
	var size = viewRender.tree.nodes.size();
	for(var i=(node.sequence+1);i<=node.shadowIndex;i++){
		if(i >= size){
			break ;
		}
		var temp = viewRender.tree.nodes.get(i);
		temp.refreshDisplay();
	}
	viewRender.refresh();
}

function overTextNode(node){
	var nodeId = node.getAttribute("nodeId");
	var cls = node.className ;
	if(cls == nodeTextStyleClassNormal){
		node.className = nodeTextStyleClassOver ;
	}
}
function outTextNode(node){
	var nodeId = node.getAttribute("nodeId");
	var cls = node.className ;
	if(cls == nodeTextStyleClassOver){
		node.className = nodeTextStyleClassNormal ;
	}
}
function clickTextNode(node){
	var nodeId = node.getAttribute("nodeId");
	var viewRender = findTreeViewRenderInstance();
	if(viewRender.currentFocusNodeId != null){
		var string = "_TEXT_NODE_"+viewRender.currentFocusNodeId ;
		var temp = document.getElementById(string);
		if(temp != null){
			temp.className = "nodeTextStyleClassNormal";
		}
	}
	var cls = node.className ;
	if(cls != nodeTextStyleClassFocus){
		node.className = nodeTextStyleClassFocus ;
	}
	
	viewRender.focusNode(nodeId);
	viewRender.clickNode(nodeId);
}